package batchcal.mas.dtc.scripts.outsource.source.impl;

import batchcal.mas.dtc.scripts.outsource.source.IDataSource;
import com.google.common.collect.Maps;
import http.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: ycy
 * @CreateDate: 2019/6/12 15:56
 */
public class BaiDuSpellCheckDataSource implements IDataSource {

  /**
   * 日志
   */
  protected static final Logger LOG = LoggerFactory.getLogger(BaiDuSpellCheckDataSource.class);

  /**
   * header
   */
  static Map<String, String>[] header = new HashMap[2];

  static {
    header[0] = new HashMap<String, String>() {
      {
        put("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1 QQBrowser/6.9.11079.201");
      }
    };

    header[1] = new HashMap<String, String>() {
      {
        put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36");
      }
    };

  }

  String baiduspellcheck = "https://www.baidu.com/s?ie=utf-8&tn=baidu&wd=%s";

  /**
   * 返回一个实例
   *
   * @return 实例
   */
  public static BaiDuSpellCheckDataSource getInstance() {
    return new BaiDuSpellCheckDataSource();
  }

  @Override
  public boolean checkCommand(String type) {
    return true;
  }

  @Override
  public Map<String, Object> getDataFromDataSource(Map<String, Object> ids, String type) {
//    for (Map.Entry<String, Object> entry : ids.entrySet()) {
//      System.out.println(entry.getKey() + " : " + entry.getValue());
//    }
    Object keyword = ids.get("keyword");
    HashMap<String, Object> stringObjectHashMap = Maps.newHashMap();
    try {
      int i = new Random().nextInt(1);
//    通过百度,获取纠错
      String res = HttpUtil.sendGet(String.format(baiduspellcheck, keyword), header[i]);
      //提取
      String matcher = matcher(res);
      stringObjectHashMap.put("res", matcher);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return stringObjectHashMap;
  }

  @Override
  public boolean setDataSourceField(String sourceMetaStr) {
    return false;
  }

  /**
   * 解析并且,获取数据
   *
   * @param result
   * @return
   */
  public static String matcher(String result) {
    Pattern compile = Pattern.compile("<div class=\"c-gap-bottom-small f13\">[.\\s\\S]+?</div>");
    Matcher matcher = compile.matcher(result);
    boolean b = matcher.find();
    String outter = null;
    if (b) {
      outter = matcher.group();
    }
    if (outter == null) {
      return null;
    }
    Pattern compile2 = Pattern.compile("<strong>.+?</strong>");
    Matcher matcher1 = compile2.matcher(outter);
    String inner = null;
    int index = 0;
    while (matcher1.find()) {
      index++;
      if (index == 2) {
        inner = matcher1.group();
      }
    }
    return inner;
  }

}
